package com.nttuan.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.nttuan.model.DataBaseHelper;
import com.nttuan.model.dao.service.ContactService;
import com.nttuan.model.dao.service.UserService;
import com.nttuan.model.entity.Contact;
import com.nttuan.model.entity.Task;
import com.nttuan.model.entity.User;
import com.nttuan.util.Support;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NT Tuan on 10/24/2014.
 */
public class ContactDao implements ContactService {
    private DataBaseHelper db;
    private UserService userService;
    private static ContactDao contactDao;

    public static ContactDao getInstance(Context context){
        if(contactDao == null)
            contactDao = new ContactDao(context);
        return  contactDao;
    }
    public ContactDao(Context context){
        try{
            userService = UserDao.getInstance(context);
            db = new DataBaseHelper(context);
        }catch (Exception e){}
    }

    @Override
    public boolean checkLeader(int taskId, int userId) {
        db.openDataBase();
    //    List<Contact> result = new ArrayList<Contact>();
        String whereClause = Task.TASK_ID + "=? and " + User.USER_ID + "=? and "+Contact.LEADER+"=1 ";
        String[] whereArgs = {taskId + "", userId+""};
        Cursor cursor = db.select(true, Contact.TABLE_NAME, null, whereClause, whereArgs, null);
        if (cursor != null)
            while (cursor.moveToNext())
                return true;
        db.close();
        return false;
    }
    @Override
    public boolean attend(int taskId, int userId, boolean attend) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        String whereClause = Task.TASK_ID + "=? and "+User.USER_ID+" =? ";
        String[] whereArgs = {taskId+"",userId+""};
        values.put(Contact.ATTEND, attend);
        boolean result = db.update(Contact.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean create(Contact contact) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        values.put(Contact.ATTEND, contact.isAttend());
        values.put(Contact.LEADER, contact.isLeader());
        values.put(Contact.IS_ACTIVE, contact.isActive());
        values.put(Task.TASK_ID, contact.getTask().getTaskId());
        values.put(User.USER_ID, contact.getUser().getUserId());
        boolean result = db.insert(Contact.TABLE_NAME, values);
        db.close();
        return result;
    }

    @Override
    public boolean update(Contact contact) {
        db.openDataBase();
        String whereClause = Contact.CONTACT_ID + "=?";
        String[] whereArgs = {contact.getContactId()+""};
        ContentValues values = new ContentValues();
        values.put(Contact.ATTEND, contact.isAttend());
        values.put(Contact.LEADER, contact.isLeader());
        values.put(Contact.IS_ACTIVE, contact.isActive());
        values.put(Task.TASK_ID, contact.getTask().getTaskId());
        values.put(User.USER_ID, contact.getUser().getUserId());
        boolean result = db.update(Contact.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean remove(int contactId) {
        db.openDataBase();
        String whereClause = Contact.CONTACT_ID + "=?";
        String[] whereArgs = {contactId+""};
        boolean result = db.delete(Contact.TABLE_NAME, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean join(int contactId, boolean isAttend) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        String whereClause = Contact.CONTACT_ID + "=?";
        String[] whereArgs = {contactId+""};
        values.put(Contact.ATTEND, isAttend);
        boolean result = db.update(Contact.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public boolean active(int contactId, boolean isActive) {
        db.openDataBase();
        ContentValues values = new ContentValues();
        String whereClause = Contact.CONTACT_ID + "=?";
        String[] whereArgs = {contactId+""};
        values.put(Contact.IS_ACTIVE, isActive);
        boolean result = db.update(Contact.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        return result;
    }

    @Override
    public List<Contact> contactsByTask(int taskId) {
        db.openDataBase();
        List<Contact> result = new ArrayList<Contact>();
        String whereClause = Task.TASK_ID + "=? and " + Contact.IS_ACTIVE +"=1";
        String[] whereArgs = {taskId+""};
        Cursor cursor =  db.select(true,Contact.TABLE_NAME,null,whereClause,whereArgs,null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItemByTask(cursor));
        db.close();
        return result;
    }
    private Contact getItemByTask(Cursor cursor){
        Contact contact = new Contact();
        contact.setContactId(cursor.getInt(cursor.getColumnIndex(Contact.CONTACT_ID)));
       contact.setAttend(Support.toActive(cursor.getString(cursor.getColumnIndex(Contact.ATTEND))));
       contact.setLeader(Support.toActive(cursor.getString(cursor.getColumnIndex(Contact.LEADER))));
        contact.setActive(Support.toActive(cursor.getString(cursor.getColumnIndex(Contact.IS_ACTIVE))));
        contact.setUser(userService.user(cursor.getInt(cursor.getColumnIndex(User.USER_ID))));
        return  contact;
    }
}
